package zuk.clipvietfun;

import java.io.File;

import zuk.clipvietfun.getdata.GetDataFromUrl;
import zuk.clipvietfun.plus.Const;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.widget.ProgressBar;

public class LoadScreen extends AsyncTask<Void, Integer, Boolean>{
	File dir;
	Context context;
	Handler starMain;
	ProgressBar pgBar;
	public LoadScreen(File dir, ProgressBar pgBar, Context context, Handler starMain) {
		this.dir = dir;
		this.context = context;
		this.starMain = starMain;
		this.pgBar = pgBar;
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		if (!dir.exists()) { 
			dir.mkdirs();
		}
		for(int i = 1 ; i <= 6 ; i++){
			if(isCancelled())
				break;
			try {
				String sClip = Const.swTab(i);
				new GetDataFromUrl(sClip,dir).getData();
			} catch (Exception e) {
				return false;
			}
			publishProgress(i);
		}
		return true;
	}
	
	@Override
	protected void onProgressUpdate(Integer... values) {
		pgBar.setProgress(values[0]);
		super.onProgressUpdate(values);
	}
	@Override
	protected void onPostExecute(Boolean result) {
		starMain.sendEmptyMessage(0);
		super.onPostExecute(result);
	}
	@Override
	protected void onCancelled() {
		super.onCancelled();
	}
	
}
